package cn.biplam.back.bean.ddl.sql2db;

import cn.biplam.back.jda.Jda;
import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 功能		sql2db包的数据操作
 * @author 	WangSong
 * @create 	2009-3-26 上午10:32:49
 * @version 1.0
 */
public class Sql2dbInfo {
	
	/**
	 * 功能	  根据数据库类型和规则类型取规则信息
	 * @param sTypeName  规则名称
	 * @param sDbType    数据库类型
	 * @return SqlResult
	 * @throws ErrorMessage 
	 */
	public static SqlResult getRuleInfo(String sTypeName,String sDbType,String precision,String scale) throws ErrorMessage{
		
		Jda mJda = new Jda(new DbResource(DbResource.RT));
		StringBuilder sb = new StringBuilder();
		sb.append(" select t.id,t.type_name,t.rule,  ");
		sb.append("        t.orders,t.dbversion      ");
		sb.append("   from PARSER_SQL2DB_RULE t      ");
		sb.append("  where t.type_name = '"+sTypeName+"' ");
		sb.append("    and t.dbversion = " + TOOLS.stringToInt(sDbType));
		if(sTypeName!=null && (sTypeName.toUpperCase().equals("NUMBER") || sTypeName.toUpperCase().equals("DECIMAL"))){
			if(TOOLS.stringToInt(precision)==0){
				sb.append("    and t.precision = 0");
			}else{
				sb.append("    and t.precision is null ");
			}
			if(TOOLS.stringToInt(scale)==0){
				sb.append("    and t.scale = 0" );
			}else{
				sb.append("    and t.scale is null " );
			}
		}
		sb.append("   order by t.orders              ");
		SqlResult sr = mJda.executeSelect(new Sql(sb.toString()));
		return sr;
	}
	
	/**
	 * 功能  取当前规则条数唯一的，规则信息
	 * @param sTypeName  规则名称
	 * @param sDbType    数据库类型
	 * @return String
	 * @throws ErrorMessage
	 */
	public static String getRule(String sTypeName,String sDbType,String precision,String scale) throws ErrorMessage{
		
		String sRule = "";
		SqlResult sr = getRuleInfo(sTypeName,sDbType,precision,scale);
		
		if(sr!=null && sr.size()>0){
			sRule = sr.get(0,"type_name");
		}
		
		return sRule;
	}
}
